/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include	<fs.h>
#include	<sig.h>
#include	<map.h>
#include	<page.h>
#include	<string.h>

int
sleep(void *chan, int pri)
{
	int n;

	cli();
	n = up->p_pri;
	if(pri <= PZERO){
		up->p_pri = pri;
		up->p_wchan = chan;
		up->p_stat = SSLEEP;
		sti();
		swtch();
		up->p_pri = n;
		return 0;
	}	
	issig(up);
	up->p_pri = pri;
	up->p_wchan = chan;
	up->p_stat = SWAIT;
	sti();
	swtch();
	issig(up);
	if(up->p_wchan)
		up->p_wchan = NULL;
	up->p_pri = n;
	up->p_stat = SREADY;
	return 0;
}

void
wakeup(void *chan)
{
        struct proc *p;

        for(p = &proc[0]; p < &proc[NPROC]; p++){
                if(p->p_stat & (SWAIT | SSLEEP) && p->p_wchan == chan)
                        pready(p);
        }
}

void
pready(struct proc *p)
{
	cli();
        p->p_pri = PUSER; 
        p->p_wchan = NULL;
        p->p_stat = SREADY;
        if(p->p_pri < up->p_pri)
                highpri++;
	sti();
}

void
freeproc(struct proc *pp)
{
	uint i, j, *p0, *p1;
	struct page_list *p;

	p0 = pp->p_pgdir;
	for(i = PG_KERN_NUM; i < PG_TBL_NUM; i++){
		if(p0[i] & PG_P){ 
			p1 = (uint *)(p0[i] & PG_ADDR_MASK);
			for(j = 0; j < PG_TBL_NUM; j++){
				if(p = page_list_get(PAGE_LIST_ADDR(p1[j]))){
					if(p->p_cnt == 1)
						mfree(p1[j] & PG_SWP ? swapmap : coremap, 1, 
							p1[j] & PG_SWP ? p->p_addr & ~PAGE_LIST_SWP: p->p_addr);
					page_list_free(p->p_addr);
				}
			}
			mfree(kernmap, 1, p0[i] & PG_ADDR_MASK);
		}
		p0[i] = 0;
	}
	pp->p_end = pp->p_brk = pp->p_start;
	if(up == pp)
		flush_tbl();
}

int
load_exec(uint off, void *vp, uint size, struct inode *ip) 
{
	uint n, bn;
	char *p, buf[BLKSIZ];

	if(!size)
		return 0;
	p = vp;
	bn = (off & BLK_MASK) >> BLK_OFF;
	if(off % BLKSIZ){
		if(log_read(bn, buf, ip, 1) != 1)
			exit(1);
		if((n = BLKSIZ - off % BLKSIZ) > size)
			n = size;
		memmove(p, buf + off % BLKSIZ, n);
		bn++;
		p += n;
		size -= n;
	}
	if(n = size / BLKSIZ){
		if(log_read(bn, p, ip, n) != n)
			exit(1);
		bn += n;
		p += n * BLKSIZ;
		size -= n * BLKSIZ; 
	}
	if(size){
		if(log_read(bn, buf, ip, 1) != 1)
			exit(1);
		memmove(p, buf, size);
	}
	return 0;
}

uint
load_argv(char *argv[])
{
	int i;
	char *p;
	uint n, *p1, argc;

	if(argv == NULL)
		return STOP;
	p = STOP;
	for(i = 0; argv[i]; i++){
		n = strlen(argv[i]) + 1;
		p -= n;
		memmove(p, argv[i], n);
	}
	n = 0;
	p1 = (uint *)((uint)p & ~0x3);
	argc = i;
	*--p1 = NULL;	
	while(--i >= 0){
		*--p1 = p + n;
		n += strlen(argv[i]) + 1;
	}
	n = (uint)p1;
	*--p1 = n; 
	*--p1 = argc;
	*--p1 = STOP;	/* eip, no use */
	return p1;
}
